<?php


namespace app\api\model;


use think\Model;
use think\Request;

class Address extends Model
{
// 表名
    protected $name = 'express_address';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public function getImageFrontAttr($value){
        $request = Request::instance();
        if (substr($request->url(), 0, 4) == '/api') {
            return cdnurl($value, true);
        }
        return $value;
    }


    public function getImageBackAttr($value){
        $request = Request::instance();
        if (substr($request->url(), 0, 4) == '/api') {
            return cdnurl($value, true);
        }
        return $value;
    }

    public function mergeIdcard($address){
        $idcard_dir = ROOT_PATH .'public'.DS.'uploads'.DS.'idcard'.DS.date('Ymd');
        //判断目录存在否，存在给出提示，不存在则创建目录
        if (!is_dir($idcard_dir)){
            mkdir($idcard_dir,0777,true);
        }
        $idcard_img = $idcard_dir.DS.md5($address['idcard']).'.jpg';
        $idcard_url = '/uploads'.DS.'idcard'.DS.date('Ymd').DS.md5($address['idcard']).'.jpg';
        $cofnig = [
            'background' =>cdnurl('/assets/img/idcard_background.png',true),
            'image' => [
                [
                    'url' => cdnurl($address['idcard_front'],true),
                    'left'=>0,
                    'top'=>0,
                    'stream'=>0,							//图片资源是否是字符串图像流
                    'right' => 0,
                    'bottom' => 0,
                    'width'=>750,
                    'height'=>460,
                    'opacity'=>100
                ],
                [
                    'url' => cdnurl($address['idcard_reverse'],true),
                    'left'=>0,
                    'top'=>470,
                    'stream'=>0,							//图片资源是否是字符串图像流
                    'right' => 0,
                    'bottom' => 0,
                    'width'=>750,
                    'height'=>460,
                    'opacity'=>100
                ]
            ],

        ];
        if(!Imgsyn::createPoster($cofnig,$idcard_img)){
            return '';
        }
        return $idcard_url;
    }
}